
Imports ccapps.persistent.authorization
Imports ccapps.persistent.blackBerry
Imports CasClientBase
Imports System.Web.Mail
Imports ccapps.batchjobs.JOB_BB


Namespace Web.page
    Public Class BlackBerry_Add_Customer
        Inherits [lib].BasePage

        Protected WithEvents Label1 As System.Web.UI.WebControls.Label
        Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
        Protected WithEvents CheckBox2 As System.Web.UI.WebControls.CheckBox
        Protected WithEvents CheckBox3 As System.Web.UI.WebControls.CheckBox
        Protected WithEvents CheckBox4 As System.Web.UI.WebControls.CheckBox
        Protected WithEvents CheckBox1 As System.Web.UI.WebControls.CheckBox
        Protected WithEvents Checkbox5 As System.Web.UI.WebControls.CheckBox

        Dim HashTable1 As New Hashtable
        Public HashTable2 As New Hashtable
        Dim Cart As DataTable
        Dim CartView As DataView
        Public custcode1 As String
        Dim model As Integer
        Protected WithEvents MyDataGrid As System.Web.UI.WebControls.DataGrid
        Protected WithEvents Datagrid10 As System.Web.UI.WebControls.DataGrid
        Protected WithEvents Label22 As System.Web.UI.WebControls.Label
        Protected WithEvents Label23 As System.Web.UI.WebControls.Label
        Dim currency As Integer




#Region " Web Form Designer Generated Code "

        'This call is required by the Web Form Designer.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

        End Sub
        Protected WithEvents Lblok As System.Web.UI.WebControls.Label
        Protected WithEvents proc As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents Txt1 As System.Web.UI.HtmlControls.HtmlInputHidden
        Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

        'NOTE: The following placeholder declaration is required by the Web Form Designer.
        'Do not delete or move it.
        Private designerPlaceholderDeclaration As System.Object

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: This method call is required by the Web Form Designer
            'Do not modify it using the code editor.
            InitializeComponent()
        End Sub

#End Region
        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' If Page.IsPostBack Then Exit Sub
            Me.PageTitle = "BB Customer Add"

            Dim msisdn As String = Request.QueryString("msisdn")

            Label22.Text = Session("compname")
            custcode1 = Request.QueryString("custcode")

            Dim temp As DataTable
            temp = getCustModel(custcode1)
            If temp.Rows().Count = 0 Then
                Message = "alert('Bu MSISDN e ait Model bulunamadi ! ')"
                Exit Sub
            Else
                model = temp.Rows(0)(1)
            End If

            If Not Page.IsPostBack Then
                Dim modeltable As DataTable
                modeltable = getBB_ModelName(Session("model"))
                Label23.Text = modeltable.Rows(0)(0)
            End If

            Dim Vproc As String
            Vproc = Me.proc.Value
            Me.proc.Value = ""
            If custcode1 <> "" And Vproc = "" Then
                Vproc = "tree"
                ''Me.Custcode.Text = custcode1
                ''Else : Me.Custcode.Text = "HATA"
            End If

            Load_HashTable()

            If Session("DG4VB_CustomerAddGrid") Is Nothing Then
                Me.Cart = New DataTable
                Me.Cart.Columns.Add(New DataColumn("CUSTCODE", GetType(String)))
                Me.Cart.Columns.Add(New DataColumn("COID", GetType(String)))
                Me.Cart.Columns.Add(New DataColumn("MSISDN", GetType(String)))
                Me.Cart.Columns.Add(New DataColumn("BBService", GetType(Boolean)))
                Me.Cart.Columns.Add(New DataColumn("saf", GetType(Boolean)))
                Me.Cart.Columns.Add(New DataColumn("ses", GetType(Boolean)))
                Me.Cart.Columns.Add(New DataColumn("cihaz", GetType(Boolean)))
                Me.Cart.Columns.Add(New DataColumn("tesis", GetType(Boolean)))
                Me.Cart.Columns.Add(New DataColumn("PROMOTION", GetType(Single)))
                Me.Cart.Columns.Add(New DataColumn("CONTRACTDATE", GetType(String)))
                Me.Cart.Columns.Add(New DataColumn("ENTDATE", GetType(String)))
                Session("DG4VB_CustomerAddGrid") = Me.Cart
            Else
                Cart = Session("DG4VB_CustomerAddGrid")
            End If


            If Not Page.IsPostBack Then
                CartView = New DataView(Cart)
                Session("SChartView") = CartView
                Datagrid10.DataSource = CartView
                'Datagrid10.DataBind()
                HashTable1.Clear()
                HashTable2.Clear()
                Session("hash1") = HashTable1
                Session("hash2") = HashTable2
                Load_HashTable()
                Load_Grid()
                Disable_Grid(-1)
                CartView.Sort = "COID"
            Else
                CartView = Session("SChartView")
                HashTable1 = Session("hash1")
                HashTable2 = Session("hash2")
                CartView.Sort = "COID"
            End If


            Select Case Vproc
                Case "save"
                    Save_Grid()
                Case "geri"
                    Response.Redirect("BlackBerry_LA_Detail_2.aspx?custcode=" & custcode1)
            End Select

        End Sub

        Sub Grid_Command(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

            ' e.Item is the row of the table where the command fired
            ' For bound columns the value is stored in the Text property of TableCell
            'Dim itemCell As TableCell = e.Item.Cells(1)
            'Dim priceCell As TableCell = e.Item.Cells(2)
            'Dim item As String = itemCell.Text
            'Dim price As String = priceCell.Text
            'Dim id As Integer

            'If e.CommandSource.CommandName = "Remove_Comp" Then
            'CartView.Delete(0)
            'Datagrid10.DataBind()
            'Calculate_Total()
            'End If

        End Sub

        Sub MyDataGrid_Page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
            Datagrid10.CurrentPageIndex = e.NewPageIndex
            BindGrid()
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Dim myDRV As DataRowView
            Dim i As Integer
            Dim _item As DataGridItem
            Dim dr As DataRow

            For i = 0 To Datagrid10.Items.Count - 1
                _item = Datagrid10.Items(i)
                Dim qtyTextBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtQty")
                Dim qtysafBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtSAF")
                Dim qtycihazBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtCIHAZ")
                Dim qtyiskontoBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtISKONTO")
                Dim bbCheckBox As CheckBox = _item.FindControl("chkBB")
                Dim handsetCheckBox As CheckBox = _item.FindControl("chkHandset")

                dr = Cart.Rows(i)
                dr(0) = bbCheckBox.Checked
                dr(1) = handsetCheckBox.Checked
                dr(5) = qtysafBox.Text
                dr(6) = qtycihazBox.Text
                dr(7) = qtyiskontoBox.Text
                dr(8) = qtyTextBox.Text
                'Cart.Rows.Add(dr)
            Next
            BindGrid()

            For Each myDRV In CartView
                'Response.Write(myDRV(2).ToString)
            Next
        End Sub
        Sub BindGrid()
            CartView.Sort = "COID"
            Datagrid10.DataSource = CartView
            Datagrid10.DataBind()
            'Disable_Grid()
        End Sub

        Sub Load_Grid()
            Dim dr As DataRow = Me.Cart.NewRow()
            Dim i As Integer

            Dim temp As DataTable
            Dim myRow As DataRow
            Dim custcode As String

            Me.Cart.Clear()
            temp = custcodeTOmsisdn(custcode1)

            For Each myRow In temp.Rows
                i = i + 1

                dr = Me.Cart.NewRow()
                dr(0) = Convert.ToString(myRow(0))
                dr(1) = Convert.ToString(myRow(1))
                dr(2) = Convert.ToString(myRow(2))
                dr(3) = Convert.ToBoolean(myRow(3))
                dr(4) = Convert.ToBoolean(myRow(4))
                dr(5) = Convert.ToBoolean(myRow(5))
                dr(6) = Convert.ToBoolean(myRow(6))
                dr(7) = Convert.ToBoolean(myRow(7))
                dr(8) = Convert.ToSingle(myRow(8))
                If myRow(9) Is DBNull.Value Then
                    dr(9) = ""
                Else
                    dr(9) = CDate(myRow(9)).ToString("dd.MM.yyyy")
                End If
                If myRow(10) Is DBNull.Value Then
                    dr(10) = ""
                Else
                    dr(10) = CDate(myRow(10)).ToString("dd.MM.yyyy")
                End If

                Me.Cart.Rows.Add(dr)
            Next
            BindGrid()
            disable_chkcontrol()
        End Sub

        Private Sub Disable_Grid(ByVal editindex As Integer)
            Dim _item As DataGridItem
            Dim i As Integer

            For i = 0 To Datagrid10.Items.Count - 1
                _item = Datagrid10.Items(i)
                If i <> editindex Then
                    'Dim qtyTextBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtQty")
                    'Dim qtysafBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtSAF")
                    'Dim qtycihazBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtCIHAZ")
                    'Dim qtyiskontoBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtISKONTO")
                    'Dim bbCheckBox As CheckBox = _item.FindControl("chkBB")
                    'Dim handsetCheckBox As CheckBox = _item.FindControl("chkHandset")
                    'If Search_Hash(_item.Cells(6).Text, HashTable1) Then
                    '    bbCheckBox.Enabled = False
                    '    handsetCheckBox.Enabled = False
                    '    qtysafBox.Enabled = False
                    '    qtycihazBox.Enabled = False
                    '    qtyiskontoBox.Enabled = False
                    '    qtyTextBox.Enabled = False
                    'End If
                End If
            Next
        End Sub


        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            Save_Grid()
        End Sub

        Sub Save_Grid()

            Dim temp As DataTable
            Dim myRow As DataRow
            Dim custcode As String
            Dim ins_var As Integer


            Dim myDRV As DataRowView
            Dim i As Integer
            Dim y As Integer
            Dim _item As DataGridItem
            Dim dr As DataRow

            Datagrid10.EditItemIndex = -1

            For i = 0 To Datagrid10.Items.Count - 1
                _item = Datagrid10.Items(i)
                Dim bbCheckBox As CheckBox = _item.FindControl("chkbb2")
                Dim safCheckBox As CheckBox = _item.FindControl("chksaf")
                Dim sesCheckBox As CheckBox = _item.FindControl("chkses")
                Dim cihazCheckBox As CheckBox = _item.FindControl("chkcihaz")
                Dim tesisCheckBox As CheckBox = _item.FindControl("chktesis")

                'Response.Write(_item.Cells(2).Text)
                Dim v_custcode1 As String = Convert.ToString(_item.Cells(2).Text)
                Dim v_coid As String = _item.Cells(3).Text
                Dim v_msisdn As String = _item.Cells(4).Text
                Dim valbb As Boolean = bbCheckBox.Checked
                Dim valsaf As Boolean = safCheckBox.Checked
                Dim valses As String = sesCheckBox.Checked
                Dim valcihaz As String = cihazCheckBox.Checked
                Dim valtesis As String = tesisCheckBox.Checked
                Dim valpromo As String = _item.Cells(10).Text
                Dim valcontractdate As DateTime
                If (_item.Cells(11).Text = "" Or _item.Cells(11).Text = "&nbsp;") Then
                    valcontractdate = Nothing
                Else
                    valcontractdate = Convert.ToDateTime(_item.Cells(11).Text)
                End If

                If Not Search_Hash(v_coid, HashTable1) Then
                    If bbCheckBox.Checked = True Then
                        ins_var = addLineBlackBerry2(custcode1, Convert.ToInt64(v_coid), v_msisdn, Convert.ToString(valbb), Convert.ToString(valsaf), Convert.ToString(valses), Convert.ToString(valcihaz), Convert.ToString(valtesis), Convert.ToInt64(valpromo), CDate(valcontractdate).ToString("dd.MM.yyyy"), GlobalVar.userID)
                        'addLineBlackBerry(ByVal parent_cust_code As String, ByVal co_id As Int64, ByVal msisdn As String, ByVal blackberry As String, ByVal handset As String, ByVal firstmonthsaf As Int64, ByVal firstmontcihaz As Int64, ByVal firstmonthiskonto As Int64, ByVal promotion As Int64, ByVal Model_id As Int64) As Integer
                        y = HashTable1.Count + 1
                        HashTable1.Add(y, v_coid)
                        'Response.Write("aaa" & HashTable1.Count)
                    End If
                ElseIf Search_Hash(v_coid, HashTable2) Then
                    'Response.Write(v_coid)
                    ins_var = modifyBBLines(Convert.ToString(valbb), Convert.ToString(valsaf), Convert.ToString(valses), Convert.ToString(valcihaz), Convert.ToString(valtesis), Convert.ToDouble(valpromo), CDate(valcontractdate).ToString("dd.MM.yyyy"), GlobalVar.userID, Convert.ToInt64(v_coid))
                    'modifyBBLines(ByVal blackberry As String, ByVal handset As String, ByVal firstmonthsaf As Int64, ByVal firstmontcihaz As Int64, ByVal firstmonthiskonto As Int64, ByVal promotion As Int64, ByVal co_id As Int64) As Integer
                End If

            Next
            HashTable2.Clear()
            Load_Grid()
            Disable_Grid(-1)
        End Sub
        Sub MyDataGrid_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
            Dim i As Integer
            i = e.Item.ItemIndex
            Datagrid10.EditItemIndex = e.Item.ItemIndex
            '_item = Datagrid10.Items(Datagrid10.EditItemIndex)
            BindGrid()
            Disable_Grid(i)

            'Dim bbCheckBox As CheckBox = _item.FindControl("chkbb")
            'Dim handsetCheckBox As CheckBox = _item.FindControl("chkHandset")
            'Dim qtysafBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtSAF")
            'Dim qtycihazBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtCIHAZ")
            'Dim qtyiskontoBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtISKONTO")
            'Dim qtyTextBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtQty")

            'Dim den As String = _item.Cells(6).Text
            'bbCheckBox.Enabled = True
            'handsetCheckBox.Enabled = True
            'qtysafBox.Enabled = True
            'qtycihazBox.Enabled = True
            'qtyiskontoBox.Enabled = True
            'qtyTextBox.Enabled = True

            'Dim _item As DataGridItem
            'Dim i As Integer

            'For i = 0 To Datagrid10.Items.Count - 1
            '    _item = Datagrid10.Items(i)
            '    Dim qtyTextBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtQty")
            '    Dim qtysafBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtSAF")
            '    Dim qtycihazBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtCIHAZ")
            '    Dim qtyiskontoBox As System.Web.UI.WebControls.TextBox = _item.FindControl("txtISKONTO")
            '    Dim bbCheckBox As CheckBox = _item.FindControl("chkBB")
            '    Dim handsetCheckBox As CheckBox = _item.FindControl("chkHandset")
            '    If Search_Hash(_item.Cells(6).Text, HashTable1) Then
            '        bbCheckBox.Enabled = False
            '        handsetCheckBox.Enabled = False
            '        qtysafBox.Enabled = False
            '        qtycihazBox.Enabled = False
            '        qtyiskontoBox.Enabled = False
            '        qtyTextBox.Enabled = False
            '    End If
            'Next

        End Sub

        Sub MyDataGrid_Cancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
            Datagrid10.EditItemIndex = -1
            BindGrid()
        End Sub
        Sub MyDataGrid_Update(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)

            Dim bbCheckBox As CheckBox = e.Item.FindControl("chkbb2")
            Dim safCheckBox As CheckBox = e.Item.FindControl("chksaf")
            Dim sesCheckBox As CheckBox = e.Item.FindControl("chkses")
            Dim cihazCheckBox As CheckBox = e.Item.FindControl("chkcihaz")
            Dim tesisCheckBox As CheckBox = e.Item.FindControl("chktesis")

            Dim vpromo As System.Web.UI.WebControls.TextBox = e.Item.Cells(10).Controls(0)
            Dim vcontractdate As System.Web.UI.WebControls.TextBox = e.Item.Cells(11).Controls(0)

            Dim i As Integer = e.Item.ItemIndex

            'Response.Write(e.Item.Cells(0).Text & e.Item.Cells(1).Text & e.Item.Cells(2).Text)
            Dim v_edit As String = e.Item.Cells(0).Text
            Dim v_update As String = e.Item.Cells(1).Text
            Dim v_custcode As String = e.Item.Cells(2).Text
            Dim v_coid As String = e.Item.Cells(3).Text
            Dim v_msisdn As String = e.Item.Cells(4).Text
            Dim valbb As Boolean = bbCheckBox.Checked
            Dim valsaf As Boolean = safCheckBox.Checked
            Dim valses As String = sesCheckBox.Checked
            Dim valcihaz As String = cihazCheckBox.Checked
            Dim valtesis As String = tesisCheckBox.Checked
            Dim valpromo As String = vpromo.Text
            Dim valcontractdate As DateTime
            If vcontractdate.Text = "" Then
                valcontractdate = CDate(Today.ToString).ToString("dd.MM.yyyy")
            Else
                valcontractdate = CDate(vcontractdate.Text).ToString("dd.MM.yyyy")
            End If
            Dim valentdate As DateTime
            If (e.Item.Cells(12).Text = "" Or e.Item.Cells(12).Text = "&nbsp;") Then
                valentdate = CDate(Today.ToString).ToString("dd.MM.yyyy")
            Else
                valentdate = CDate(e.Item.Cells(12).Text).ToString("dd.MM.yyyy")
            End If
            ' with a database, we'd use an update command.  Since we're using an in-memory
            ' DataTable, we'll delete the old row and replace it with a new one
            ' remove old entry
            CartView.RowFilter = "COID='" & v_coid & "'"
            If CartView.Count > 0 Then
                CartView.Delete(0)
            End If
            CartView.RowFilter = ""

            'add new entry
            Dim dr As DataRow = Cart.NewRow()
            dr(0) = v_custcode
            dr(1) = v_coid
            dr(2) = v_msisdn
            dr(3) = Convert.ToBoolean(valbb)
            dr(4) = Convert.ToBoolean(valsaf)
            dr(5) = Convert.ToBoolean(valses)
            dr(6) = Convert.ToBoolean(valcihaz)
            dr(7) = Convert.ToBoolean(valtesis)
            dr(8) = valpromo
            dr(9) = CDate(valcontractdate).ToString("dd.MM.yyyy") 'valcontractdate
            dr(10) = CDate(valentdate).ToString("dd.MM.yyyy") 'valentdate

            Cart.Rows.Add(dr)

            Datagrid10.EditItemIndex = -1
            BindGrid()
            Disable_Grid(-1)

            i = HashTable2.Count + 1
            HashTable2.Add(i, v_coid)
            disable_chkcontrol()
            'Response.Write("aaa" & HashTable2.Count)
        End Sub
        Private Sub disable_chkcontrol()
            Dim i As Integer
            Dim _item As DataGridItem

            For i = 0 To Datagrid10.Items.Count - 1
                _item = Datagrid10.Items(i)
                Dim bbCheckBox As CheckBox = _item.FindControl("chkbb2")
                Dim safCheckBox As CheckBox = _item.FindControl("chksaf")
                Dim sesCheckBox As CheckBox = _item.FindControl("chkses")
                Dim cihazCheckBox As CheckBox = _item.FindControl("chkcihaz")
                Dim tesisCheckBox As CheckBox = _item.FindControl("chktesis")

                bbCheckBox.Enabled = False
                safCheckBox.Enabled = False
                sesCheckBox.Enabled = False
                cihazCheckBox.Enabled = False
                tesisCheckBox.Enabled = False
            Next
        End Sub

        Private Sub Datagrid10_ItemDataBound(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles Datagrid10.ItemDataBound

            'Dim bbCheckBox As CheckBox = e.Item.FindControl("chkbb")
            'Dim handsetCheckBox As CheckBox = e.Item.FindControl("chkHandset")
            'Dim qtysafBox As System.Web.UI.WebControls.TextBox = e.Item.FindControl("txtSAF")
            'Dim qtycihazBox As System.Web.UI.WebControls.TextBox = e.Item.FindControl("txtCIHAZ")
            'Dim qtyiskontoBox As System.Web.UI.WebControls.TextBox = e.Item.FindControl("txtISKONTO")
            'Dim qtyTextBox As System.Web.UI.WebControls.TextBox = e.Item.FindControl("txtQty")

            Dim a1 As String = e.Item.Cells(2).Text()
            Dim a2 As String = e.Item.Cells(3).Text()
            Dim a3 As String = e.Item.Cells(4).Text()
            Dim a4 As String = e.Item.Cells(5).Text()
            If Search_Hash(e.Item.Cells(3).Text, HashTable1) Then

                e.Item.BackColor = System.Drawing.Color.Red
                'e.Item.Cells(4).Enabled = False
                'e.Item.Cells(5).Enabled = False
                'e.Item.Cells(6).Enabled = False
                'bbCheckBox.Enabled = False
                'handsetCheckBox.Enabled = False
                'qtysafBox.Enabled = False
                'qtycihazBox.Enabled = False
                'qtyiskontoBox.Enabled = False
                'qtyTextBox.Enabled = False
            End If

        End Sub

        Sub Load_HashTable()
            Dim temp As DataTable
            Dim myRow As DataRow
            Dim i As Integer = 1

            HashTable1.Clear()
            temp = getBBLines(custcode1)

            For Each myRow In temp.Rows
                HashTable1.Add(i, Convert.ToString(myRow(0)))
                i = i + 1
            Next

        End Sub

        Function Search_Hash(ByVal valueToFind As String, ByVal Hashbtableval As Hashtable) As Boolean
            If Hashbtableval.ContainsValue(valueToFind) Then
                'Response.Write(valueToFind & "----->" & True)
                Return True
            Else
                Return False
                'Response.Write(valueToFind & "----->" & False)
            End If
        End Function

        Sub Add_HashTable(ByVal valueToAdd As String, ByVal hashtableval As Hashtable)
            Dim temp As DataTable
            Dim myRow As DataRow
            'HashTable1.Clear()
            hashtableval.Add(hashtableval.Count() + 1, valueToAdd)
        End Sub

        Private Sub Datagrid10_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Datagrid10.SelectedIndexChanged

        End Sub
    End Class
End Namespace
